#ifndef _BSP_DS18B20_H_
#define _BSP_DS18B20_H_
#include "rtconfig.h"
#include "stm32f10x.h"
#include "main.h"

/****************下面做一些有关DS18B20管脚的宏定义*************************/
#define			DS18B20_IN_WATER_TEMP_PORT				GPIOB
#define			DS18B20_IN_WATER_TEMP_PIN				GPIO_Pin_11
#define			DS18B20_IN_WATER_TEMP_CLK				RCC_APB2Periph_GPIOB

#define			DS18B20_OUT_WATER_TEMP_PORT				GPIOB
#define			DS18B20_OUT_WATER_TEMP_PIN				GPIO_Pin_10
#define			DS18B20_OUT_WATER_TEMP_CLK				RCC_APB2Periph_GPIOB

#define			DS18B20_OUT_ENVIRON_TEMP_PORT				GPIOB
#define			DS18B20_OUT_ENVIRON_TEMP_PIN				GPIO_Pin_2
#define			DS18B20_OUT_ENVIRON_TEMP_CLK				RCC_APB2Periph_GPIOB

#define			DS18B20_OUT_TUBE_TEMP_PORT				GPIOB
#define			DS18B20_OUT_TUBE_TEMP_PIN				GPIO_Pin_1
#define			DS18B20_OUT_TUBE_TEMP_CLK				RCC_APB2Periph_GPIOB

#define			DS18B20_OUT_COMPRESSOR_TEMP_PORT				GPIOB
#define			DS18B20_OUT_COMPRESSOR_TEMP_PIN				GPIO_Pin_0
#define			DS18B20_OUT_COMPRESSOR_TEMP_CLK				RCC_APB2Periph_GPIOB   //因为要取消JTDI

//管脚操作宏函数
#define			DS18B20_IN_WATER_TEMP_HIGH()				GPIO_SetBits(DS18B20_IN_WATER_TEMP_PORT,DS18B20_IN_WATER_TEMP_PIN)   //这个脚为写管脚
#define			DS18B20_IN_WATER_TEMP_LOW()				GPIO_ResetBits(DS18B20_IN_WATER_TEMP_PORT,DS18B20_IN_WATER_TEMP_PIN)

#define			DS18B20_OUT_WATER_TEMP_HIGH()				GPIO_SetBits(DS18B20_OUT_WATER_TEMP_PORT,DS18B20_OUT_WATER_TEMP_PIN)   //这个脚为写管脚
#define			DS18B20_OUT_WATER_TEMP_LOW()				GPIO_ResetBits(DS18B20_OUT_WATER_TEMP_PORT,DS18B20_OUT_WATER_TEMP_PIN)

#define			DS18B20_OUT_ENVIRON_TEMP_HIGH()				GPIO_SetBits(DS18B20_OUT_ENVIRON_TEMP_PORT,DS18B20_OUT_ENVIRON_TEMP_PIN)   //这个脚为写管脚
#define			DS18B20_OUT_ENVIRON_TEMP_LOW()				GPIO_ResetBits(DS18B20_OUT_ENVIRON_TEMP_PORT,DS18B20_OUT_ENVIRON_TEMP_PIN)

#define			DS18B20_OUT_TUBE_TEMP_HIGH()				GPIO_SetBits(DS18B20_OUT_TUBE_TEMP_PORT,DS18B20_OUT_TUBE_TEMP_PIN)   //这个脚为写管脚
#define			DS18B20_OUT_TUBE_TEMP_LOW()				GPIO_ResetBits(DS18B20_OUT_TUBE_TEMP_PORT,DS18B20_OUT_TUBE_TEMP_PIN)

#define			DS18B20_OUT_COMPRESSOR_TEMP_HIGH()				GPIO_SetBits(DS18B20_OUT_COMPRESSOR_TEMP_PORT,DS18B20_OUT_COMPRESSOR_TEMP_PIN)   //这个脚为写管脚
#define			DS18B20_OUT_COMPRESSOR_TEMP_LOW()				GPIO_ResetBits(DS18B20_OUT_COMPRESSOR_TEMP_PORT,DS18B20_OUT_COMPRESSOR_TEMP_PIN)

#define			DS18B20_IN_WATER_TEMP()					GPIO_ReadInputDataBit(DS18B20_IN_WATER_TEMP_PORT,DS18B20_IN_WATER_TEMP_PIN)
#define			DS18B20_OUT_WATER_TEMP()					GPIO_ReadInputDataBit(DS18B20_OUT_WATER_TEMP_PORT,DS18B20_OUT_WATER_TEMP_PIN)
#define			DS18B20_OUT_ENVIRON_TEMP()					GPIO_ReadInputDataBit(DS18B20_OUT_ENVIRON_TEMP_PORT,DS18B20_OUT_ENVIRON_TEMP_PIN)
#define			DS18B20_OUT_TUBE_TEMP()					GPIO_ReadInputDataBit(DS18B20_OUT_TUBE_TEMP_PORT,DS18B20_OUT_TUBE_TEMP_PIN)
#define			DS18B20_OUT_COMPRESSOR_TEMP()					GPIO_ReadInputDataBit(DS18B20_OUT_COMPRESSOR_TEMP_PORT,DS18B20_OUT_COMPRESSOR_TEMP_PIN)

/**
*	@brief	精准延时函数(这里的DS18B20线程得设置为最高)
*	@param	us 延时的us数
*	@retval 无
*/
static __inline void SysTick_Delay_Us(uint32_t us)
{
    uint32_t delta=0;   //alpha  beta
    //SysTick->LOAD=72000000/RT_TICK_PER_SECOND   ....
    us = us * (SysTick->LOAD / (1000000 / RT_TICK_PER_SECOND));    //这样可以做到灵活
    delta = SysTick->VAL;    //这个值是SysTick的当前值,每1/72000000 秒会减一次

    while (delta - SysTick->VAL < us)
    {
        ;
    }
}


//void SysTick_Delay_Us(uint32_t us);
void	DS18B20_GPIO_Init(void);
void DS18B20_Temp(void);

extern U8_FLOAT_Typedef ds18b20_dat[5];

#endif

